package cn.zmit.kuxi.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.fragment.HisAllFragment;
import cn.zmit.kuxi.fragment.HisWinFragment;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.request.RequestTask;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

public class HisCenterActivity extends BaseActivity {
	@ViewInject(R.id.rg_toggle_center)
	private RadioGroup rg_toggle_center;
	@ViewInject(R.id.rb_all)
	private RadioButton rb_all;
	@ViewInject(R.id.rb_win)
	private RadioButton rb_win;
	@ViewInject(R.id.view_indicator_all)
	private View view_all;
	@ViewInject(R.id.view_indicator_win)
	private View view_win;

	private FragmentManager fragmentManager;
	private HisWinFragment hisWinFragment;
	private HisAllFragment hisAllFragment;

	private String name;
	private String phone;
	private String avatar;
	private String win_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_his_center);
		ViewUtils.inject(this);
		// 根据获得的id改变标题
		userid = PreferenceHelper.readString(this,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID, "");
		win_id = getIntent().getStringExtra("win_id");
		if (userid.equals(win_id)) {
			setTitleView("我的个人中心", true);
		} else {
			setTitleView("TA的个人中心", true);
		}
		// 获取fragmentManager
		fragmentManager = getFragmentManager();
		// 默认选中第一个
		setTabSelection(0);
		// onCreate request user's img,name,phonenum
		RequestTask.getInstance().getHistoryRecord(this, win_id, 1 + "",
				new onRequestListener());

	}

	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);
			JsonData jsonData = JsonData.create(response);
			JsonData user_info = jsonData.optJson("user_info");
			avatar = user_info.optString("avatar");
			name = user_info.optString("name");
			phone = user_info.optString("phone");
			Log.e("ava,name,phone", avatar + "---" + name + "---" + phone);

			// consider the thread
			ImageView img_user = (ImageView) findViewById(R.id.img_user);
			TextView tv_user_name = (TextView) findViewById(R.id.tv_user_name);
			TextView tv_user_phonenum = (TextView) findViewById(R.id.tv_user_phonenum);
			if (avatar != "" && avatar.length() != 0) {
				ImageDisplayer.getInstance().dispalyImageWithRadius(
						Url.IMAGR_ROOT + avatar, img_user, 0);
			} else {
				img_user.setImageResource(R.drawable.user_pic_def);
			}
			tv_user_name.setText("昵称：" + name);
			if (userid.equals(win_id)) {
				tv_user_phonenum.setText(phone);
			} else {
				tv_user_phonenum.setText("号码：" + phone);
			}
		}
	}

	// toogleIndicator =the view bottom
	private void toggleIndicator(int id) {
		switch (id) {
		case R.id.rb_all:
			view_all.setBackgroundColor(getResources().getColor(R.color.red2));
			view_win.setBackgroundColor(getResources().getColor(R.color.gray));
			break;
		case R.id.rb_win:
			view_all.setBackgroundColor(getResources().getColor(R.color.gray));
			view_win.setBackgroundColor(getResources().getColor(R.color.red2));
			break;
		default:
			break;
		}
	}

	// the method of app.fragment v4's fragment does not has this method
	private void hideFragments(FragmentTransaction transaction) {
		if (hisWinFragment != null) {
			transaction.hide(hisWinFragment);
		}
		if (hisAllFragment != null) {
			transaction.hide(hisAllFragment);
		}
	}

	// clear the background to black
	private void clear() {
		rb_all.setTextColor(getResources().getColor(R.color.black));
		rb_win.setTextColor(getResources().getColor(R.color.black));
	}

	// select tab
	private void setTabSelection(int index) {
		clear();
		// begin transaction
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		// hide all the fragments
		hideFragments(transaction);
		switch (index) {
		case 0:
			rb_all.setTextColor(getResources().getColor(R.color.red2));
			if (hisAllFragment == null) {
				hisAllFragment = new HisAllFragment();
				transaction.add(R.id.fl_fragment, hisAllFragment);
			} else {
				transaction.show(hisAllFragment);
			}
			break;
		case 1:
			rb_win.setTextColor(getResources().getColor(R.color.red2));
			if (hisWinFragment == null) {
				hisWinFragment = new HisWinFragment();
				transaction.add(R.id.fl_fragment, hisWinFragment);
			} else {
				transaction.show(hisWinFragment);
			}
			break;
		default:
			break;
		}
		transaction.commit();
	}

	@OnClick({ R.id.rb_all, R.id.rb_win })
	private void OnClick(View v) {
		switch (v.getId()) {
		case R.id.rb_all:
			toggleIndicator(R.id.rb_all);
			setTabSelection(0);
			break;
		case R.id.rb_win:
			toggleIndicator(R.id.rb_win);
			setTabSelection(1);
			break;
		default:
			break;
		}
	}
}
